'use strict';
let choose_year = null,
    choose_month = null;
const conf = {
    data: {
        hasEmptyGrid: false,
        endHasEmptyGrid: false,
        showPicker: false
    },
    onLoad() {
        const date = new Date();
        const cur_year = date.getFullYear();
        const cur_month = date.getMonth() + 1;
        const weeks_ch = ['日', '一', '二', '三', '四', '五', '六', '周'];
        this.calculateDays(cur_year, cur_month);
        this.calculateEmptyGrids(cur_year, cur_month);
        this.setData({
            cur_year,
            cur_month,
            weeks_ch
        });
    },
    /**
     * 当前月 有几天
     */
    getThisMonthDays(year, month) {
        return new Date(year, month, 0).getDate();
    },
    /**
     * 一月的第一天是周几
     */
    getFirstDayOfWeek(year, month) {
        return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },
    /**
     * 添加空百天数
     */
    calculateEmptyGrids(year, month) {
        const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
        const lastMonthDatas = this.getThisMonthDays(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1);
        const thisMonthDatas = this.getThisMonthDays(year, month);
        let empytGrids = [];
        if (firstDayOfWeek > 0) {
            for (let i = 0; i < firstDayOfWeek; i++) {
                empytGrids.push({
                    day: lastMonthDatas - firstDayOfWeek + i + 1
                });
            }

        }
        const endDayMoreData = 7 - (thisMonthDatas + firstDayOfWeek + 1) % 7
        console.log("endDayMoreData==" + endDayMoreData);
        let endEmpytGrids = [];
        if (endDayMoreData > 0 && endDayMoreData < 6) {

            for (var i = 0; i < endDayMoreData + 1; i++) {
                endEmpytGrids.push({
                    day: i + 1
                });
            }

        } else {
            this.setData({
                endHasEmptyGrid: false,
                endEmpytGrids: []
            });
        }
        var days = this.data.days

        this.setData({
            hasEmptyGrid: empytGrids.length > 0,
            endHasEmptyGrid: endEmpytGrids.length > 0,
            empytGrids: empytGrids,
            endEmpytGrids: endEmpytGrids,
            showLast: empytGrids.length + endEmpytGrids.length + days.length > 35,
        });


    },
    /**
     * 天数
     */
    calculateDays(year, month) {
        let days = [];

        const thisMonthDays = this.getThisMonthDays(year, month);

        for (let i = 1; i <= thisMonthDays; i++) {
            days.push({
                day: i,
                choosed: false
            });
        }

        this.setData({
            days
        });
    },
    handleCalendar(e) {
        const handle = e.currentTarget.dataset.handle;
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;
        if (handle === 'prev') {
            let newMonth = cur_month - 1;
            let newYear = cur_year;
            if (newMonth < 1) {
                newYear = cur_year - 1;
                newMonth = 12;
            }

            this.calculateDays(newYear, newMonth);
            this.calculateEmptyGrids(newYear, newMonth);

            this.setData({
                cur_year: newYear,
                cur_month: newMonth
            });

        } else {
            let newMonth = cur_month + 1;
            let newYear = cur_year;
            if (newMonth > 12) {
                newYear = cur_year + 1;
                newMonth = 1;
            }

            this.calculateDays(newYear, newMonth);
            this.calculateEmptyGrids(newYear, newMonth);

            this.setData({
                cur_year: newYear,
                cur_month: newMonth
            });
        }
    },
    /**
     * 点击日
     */
    tapDayItem(e) {
        const idx = e.currentTarget.dataset.idx;
        const days = this.data.days;
        if (days[idx].choosed) {
            return;
        }
        for (var i = 0; i < days.length; i++) {
            days[i].choosed = false
        }
        days[idx].choosed = true
        this.setData({
            days,
        });
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;
        var day = days[idx].day
        console.log("选中日期" + cur_year + "年" + cur_month + "月" + day + "日")
    },
    /**
     * 点击月
     */
    chooseYearAndMonth() {
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;
        console.log("选中日期" + cur_year + "年" + cur_month + "月")

    },


    chooseWeek: function (event) {
        var id = event.currentTarget.id
        console.log("id==" + id)
        var empytGrids = this.data.empytGrids
        var endEmpytGrids = this.data.endEmpytGrids
        var days = this.data.days
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;
        var startY = ""//开始年
        var startM = ""//开始月
        var startD = ""//开始日
        var endY = ""//结束年
        var endM = ""//结束月
        var endD = ""//结束日
        var totalLength=days.length+empytGrids.length+endEmpytGrids.length

        switch (id) {
            case "1":
                if (empytGrids.length > 0) {

                    if (cur_month == 1) {
                        startY = cur_year - 1
                        startM = 12
                    } else {
                        startY = cur_year
                        startM = cur_month - 1
                    }
                    startD = empytGrids[0].day
                    endY = cur_year
                    endM = cur_month
                    endD = days[6 - empytGrids.length].day
                } else {
                    startY = cur_year
                    startM = cur_month
                    console.log("day==" + JSON.stringify(days[0]))
                    startD = days[0].day
                    endY = cur_year
                    endM = cur_month
                    endD = days[6].day

                }
                break
            case "5":
                if (totalLength>5*7)
                {
                    startY = cur_year
                    startM = cur_month
                    endY = cur_year
                    endM = cur_month
                    var startIndex = (id - 1) * 7 - empytGrids.length
                    startD = days[startIndex].day
                    endD = days[startIndex + 6].day
                }else
                {
                    startY = cur_year
                    startM = cur_month
                    if (cur_month==12)
                    {
                        endY = cur_year+1
                        endM = 1
                    }else
                    {
                        endY = cur_year
                        endM = cur_month+1
                    }
                  
                    var startIndex = (id - 1) * 7 - empytGrids.length
                    startD = days[startIndex].day
                    endD = endEmpytGrids[endEmpytGrids.length-1].day
                }

                break
            case "6":
                startY = cur_year
                startM = cur_month
                if (cur_month == 12) {
                    endY = cur_year + 1
                    endM = 1
                } else {
                    endY = cur_year
                    endM = cur_month + 1
                }

                var startIndex = (id - 1) * 7 - empytGrids.length
                startD = days[startIndex].day
                endD = endEmpytGrids[endEmpytGrids.length - 1].day
                break
            default:
                startY = cur_year
                startM = cur_month
                endY = cur_year
                endM = cur_month
                var startIndex = (id - 1) * 7 - empytGrids.length
                startD = days[startIndex].day
                endD = days[startIndex + 6].day
                break
        }
        console.log("satrt==" + startY + "-" + startM + "-" + startD + "emd==" + endY + "-" + endM + "-" + endD )
    }


};

Page(conf);
